package com.happystudy.serviceImpl;

import com.happpystudy.model.courseItem.SubmitRecord;
import com.happpystudy.service.FileService;
import com.happpystudy.service.SubmitRecordService;
import com.happystudy.mapper.SubmitRecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class SubmitRecordServiceImpl implements SubmitRecordService {

    @Resource
    FileService fileService;

    @Resource
    SubmitRecordMapper submitRecordMapper;

    @Override
    public int doStudentUploadFile(SubmitRecord submitRecord, MultipartFile file){
        int result = 0;
        String fullPath=null;
        try {
            fullPath  = fileService.doSave(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (fullPath!=null){
            submitRecord.setAttachment(file.getOriginalFilename());
            submitRecord.setUuid(fullPath);
            submitRecord.setContentType(file.getContentType());
            submitRecord.setUploadTime(new Date());
            result = this.insertSubmitRecord(submitRecord);
        }


        return result;
    }


    @Override
    public int doUpdateStudentUploadFile(SubmitRecord submitRecord, MultipartFile file){

        submitRecordMapper.deleteByItemId(submitRecord.getItemId());
        return this.doStudentUploadFile(submitRecord,file);

    }

    @Override
    public int deleteSubmitRecord(Integer itemId){
        return submitRecordMapper.deleteByItemId(itemId);
    }



    @Override
    public int insertSubmitRecord(SubmitRecord submitRecord){
        return submitRecordMapper.insertSelective(submitRecord);
    }

    @Override
    public List<SubmitRecord> selectAllSubmitRecordByItemId(Integer itemId){
        return submitRecordMapper.selectByItemId(itemId);
    }

    @Override
    public SubmitRecord selectByCourseInfo(SubmitRecord submitRecord){
        return submitRecordMapper.selectByCourseInfo(submitRecord);
    }
}
